package cn.edu.buu.news.controller;

import cn.edu.buu.news.entity.Task;
import cn.edu.buu.news.service.TaskService;
import cn.edu.buu.news.vo.PageQuery;
import cn.edu.buu.news.vo.PageResult;
import cn.edu.buu.news.vo.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/task")
public class TaskController {

    @Autowired
    private TaskService taskService;

    // 新增任务
    @PostMapping
    public Result add(@RequestBody Task task) {
        taskService.add(task);
        return Result.success();
    }

    // 删除任务
    @DeleteMapping("/{task_id}")
    public Result delete(@PathVariable Long task_id) {
        taskService.deleteById(task_id);
        return Result.success();
    }

    // 更新任务
    @PutMapping
    public Result update(@RequestBody Task task) {
        taskService.update(task);
        return Result.success();
    }

    // 根据ID查询
    @GetMapping("/{task_id}")
    public Result<Task> getById(@PathVariable Long task_id) {
        return Result.success(taskService.findById(task_id));
    }

    // 查询所有
    @GetMapping
    public Result<List<Task>> getAll() {
        return Result.success(taskService.findAll());
    }

    // 分页查询
    @PostMapping("/list")
    public Result<PageResult<Task>> getByPage(@RequestBody PageQuery query) {
        return Result.success(taskService.findByPage(query));
    }
}